<?php
/**
 * Created by PhpStorm.
 * User: w-pc
 * Date: 2017/03/13
 * Time: 16:57
 */

namespace Common\Model;
use Think\Model;

class DiaryModel extends Model{
    private $_db = '';

    public function __construct() {
        $this->_db = M('diary');
    }
    public function get_DiaryList($d_state){
        if($d_state==0){
            $diary_list1 = $this->_db->where(array('d_state'=>'2'))->select();
            $diary_list2 = $this->_db->where(array('d_state'=>'1'))->select();
            $diary_list = array_merge($diary_list1,$diary_list2);
        }else{
            $where['d_state'] = $d_state;
            $diary_list = $this->_db->where($where)->select();
        }
        $diary_list = array_reverse($diary_list);
        return $diary_list;
    }
    public function get_DiaryByOne($d_id){
        $where['d_id'] = $d_id;
        $diary = $this->_db->where($where)->find();
        return $diary;
    }
    public function set_StickState($d_id){
        $diary = $this->get_DiaryByOne($d_id);
        if($diary['d_state']==2){
            $diary['d_state'] = 1;
        }else if($diary['d_state']==3){
            $diary['d_state'] = 1;
        }else if($diary['d_state']==1){
            $diary['d_state'] = 2;
        }else{
            $diary['d_state'] = 1;
        }
        $this->update_Diary($d_id,$diary);
    }
    public function update_Diary($d_id,$data){
        $where['d_id'] = $d_id;
        $this->_db->where($where)->save($data);
    }
    public function save_Diary($data){
        $d_id = $this->_db->where()->add($data);
        return $d_id;
    }
    public function delete($d_id){
        $diary = $this->get_DiaryByOne($d_id);
        $diary['d_state'] = 3;
        $this->update_Diary($d_id,$diary);
    }
}